package com.appbee.media.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.design.PageStyle;
import com.appbee.media.client.design.ThumbnailCellListResources;
import com.appbee.media.client.presenter.MediaThumbnailActivity;
import com.appbee.media.shared.MediaProxy;
import com.appbee.user.client.AccessControl;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
public class MediaThumbnailView extends Composite implements MediaThumbnailActivity.View
{

	private static MediaThumbnailViewUiBinder uiBinder = GWT
			.create(MediaThumbnailViewUiBinder.class);

	interface MediaThumbnailViewUiBinder extends
			UiBinder<Widget, MediaThumbnailView>
	{
	}

	@UiField(provided=true)
	PageStyle style;
	
	@UiField
	FlowPanel buttonPanel;
	
	Button uploadButton;
	
	@UiField(provided=true)
	CellList<MediaProxy> cellList;
	
	@UiField(provided=true)
	SimplePager pager;
	
	private final ThumbnailCellListResources thumbnailRes;
	private final MediaHtmlTemplate template;

	private MediaThumbnailActivity activity;
	
	@Inject
	public MediaThumbnailView(PageStyle style,
			ThumbnailCellListResources thumbnailRes,
			MediaHtmlTemplate template,
 AccessControl accessControl)
	{
		this.style = style;
		this.thumbnailRes = thumbnailRes;
		this.template = template;
		this.cellList = new CellList<MediaProxy>(new MediaThumbnailCell(),thumbnailRes);
		this.pager = new SimplePager();
		pager.setDisplay(cellList);
		initWidget(uiBinder.createAndBindUi(this));
		
		if (accessControl.isMemberOrAdmin())
		{
			uploadButton = new Button("Setja inn myndir");
			buttonPanel.add(uploadButton);
			uploadButton.addClickHandler(new ClickHandler()
			{
				
				@Override
				public void onClick(ClickEvent event)
				{
					activity.goToUpload();
				}
			});
		}
	}

	public class MediaThumbnailCell extends AbstractCell<MediaProxy>
	{
		public MediaThumbnailCell()
		{
			super("click");

		}
		@Override
		public void render(Context context, MediaProxy value, SafeHtmlBuilder sb)
		{
			if(value!=null)
			{
				sb.append(template.img(value.getName()==null?"":value.getName(), "/thumbnail?id="+value.getId()));
				sb.append(template.button(thumbnailRes.cellListStyle()
						.viewButton(), "Skoða"));
			}
		}
		
		@Override
		public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context,
				Element parent, MediaProxy value, NativeEvent event,
				ValueUpdater<MediaProxy> valueUpdater)
		{
			String type = event.getType();
			Element target = event.getEventTarget().cast();
			if("click".equals(type) && "BUTTON".equals(target.getTagName()))
			{
				activity.mediaViewClick(value,context.getIndex());
			}
			super.onBrowserEvent(context, parent, value, event, valueUpdater);
		}
		
	}

	@Override
	public HasData<MediaProxy> getHasData()
	{
		return cellList;
	}

	@Override
	public void setActivity(MediaThumbnailActivity activity)
	{
		this.activity = activity;
	}

}
